<script setup>
import { RouterLink, RouterView } from "vue-router";
import { Search, ArrowDown } from "@element-plus/icons-vue";
import { ref, onMounted } from "vue";
import { useUserStore } from "@/stores/user";
import { useBlogStore } from "@/stores/blog";
import LoginDialog from "@/components/LoginDialog.vue";
import { ElMessage } from "element-plus";
import { getUserInfo } from "@/api/user";
import RegisterDialog from "@/components/RegisterDialog.vue";
import { searchBlogs, searchUsers } from "@/api/search";

const BlogStore = useBlogStore();

const searchText = ref("");
const searchType = ref("blog");

const searchOptions = [
  { value: "blog", label: "搜索博客" },
  { value: "user", label: "搜索用户" }
];

const handleSearch = async () => {
  BlogStore.getBlogs(searchText.value, searchType.value, 1, 10);
};

const userStore = useUserStore();
const showRegister = ref(false);
const showLoginDialog = ref(false);

onMounted(async () => {
  await userStore.initUserInfo();
});

const showLogin = () => {
  showLoginDialog.value = true;
};

const showRegisterDialog = () => {
  showRegister.value = true;
  showLoginDialog.value = false;
};

const handleLoginSuccess = () => {
  showLoginDialog.value = false;
  ElMessage.success("登录成功");
};

const handleLogout = async () => {
  try {
    await userStore.logoutAction();
    ElMessage.success("退出成功");
  } catch (error) {
    console.error("退出失败:", error);
  }
};

const handleRegisterSuccess = () => {
  showRegister.value = false;
  showLoginDialog.value = true;
};

// onMounted(async () => {
//   try {
//     const res = await getUserInfo()
//     userStore.userInfo = res.data
//   } catch (error) {
//     console.error('获取用户信息失败:', error)
//   }
// })
</script>

<template>
  <el-container>
    <el-header>
      <div class="header-content">
        <div class="header-left">
          <img class="logo" alt="Logo" src="@/assets/logo.svg" />
        </div>

        <div class="search-container">
          <el-input
            v-model="searchText"
            :placeholder="searchType === 'blog' ? '搜索博客内容...' : '搜索用户名...'"
            class="search-input"
          >
            <template #prefix>
              <el-select v-model="searchType" class="search-type-select">
                <el-option
                  v-for="item in searchOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
            <template #append>
              <el-button :icon="Search" @click="handleSearch" />
            </template>
          </el-input>
        </div>

        <div class="header-right">
          <template v-if="userStore.userInfo">
            <el-dropdown>
              <span class="user-info">
                <el-avatar :src="userStore.userInfo.avatarUrl" />
                {{ userStore.userInfo.username }}
                <el-icon>
                  <ArrowDown />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <template v-else>
            <el-button type="primary" @click="showLogin">登录</el-button>
            <el-button @click="showRegisterDialog">注册</el-button>
          </template>
        </div>
        <el-menu mode="horizontal" router class="main-nav">
          <template v-if="userStore.isAdmin">
            <el-menu-item index="/admin/users">用户管理</el-menu-item>
            <el-menu-item index="/admin/blogs">博客管理</el-menu-item>
          </template>
          <template v-else>
            <el-menu-item index="/">博客</el-menu-item>
            <el-menu-item index="/about">讨论</el-menu-item>
            <el-menu-item index="/user">用户</el-menu-item>
            <el-menu-item index="/person">个人中心</el-menu-item>
          </template>
        </el-menu>
      </div>
    </el-header>

    <el-main>
      <RouterView />
    </el-main>

    <RegisterDialog v-model:dialogVisible="showRegister" @register-success="handleRegisterSuccess" />
    <LoginDialog v-model:dialogVisible="showLoginDialog" @login-success="handleLoginSuccess" />
  </el-container>
</template>

<style scoped>
.el-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.el-main {
  width: 1200px;
  padding: 0;
}

.el-header {
  padding: 0;
  height: 60px;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  width: 40px;
  height: 40px;
}

.search-container {
  flex: 1;
  max-width: 800px;
  margin: 0 40px;
}

.search-input {
  width: 100%;
}

.search-type-select {
  width: 110px;
}

.search-type-select :deep(.el-input__wrapper) {
  background-color: transparent;
  box-shadow: none;
}

.search-input :deep(.el-input__wrapper) {
  padding-left: 0;
}

.search-input :deep(.el-input-group__append) {
  background-color: var(--el-color-primary);
  border-color: var(--el-color-primary);
  padding: 0 20px;
}

.search-input :deep(.el-input-group__append) .el-button {
  border: none;
  background: transparent;
  color: white;
}

.main-nav {
  border-bottom: none;
}

.header-right {
  display: flex;
  gap: 12px;
  align-items: center;
}

.el-main {
  padding-top: 20px;
  margin: 0 auto;
  /* background-color: #f5f5f5; */
}
</style>
